package com.yhc.mmm.utils;

import android.support.v4.app.Fragment;
import android.util.SparseArray;

import com.yhc.mmm.fragments.BargainSaleFragment;
import com.yhc.mmm.fragments.BrandSaleFragment;
import com.yhc.mmm.fragments.LoginFragment;
import com.yhc.mmm.fragments.mainfragments.MainSaleFragment;
import com.yhc.mmm.fragments.OnSaleFragment;

/**
 * Created by My on 2016/9/2.
 */
public class FragmentFactory {
    private static final int FRAGMENT_ONSALE = 0;
    private static final int FRAGMENT_BARGAINSALE = 1;
    private static final int FRAGMENT_MAINSALE = 2;
    private static final int FRAGMENT_BRANDSALE = 3;
    private static final int FRAGMENT_LOGIN = 4;

    //SparseArray:key是整形的key,根据整形的key来获取一个对象,效率比较高.
    private static SparseArray<Fragment> fragments = new SparseArray<>();

    //创建一个工厂方法,用来创建一个Fragment对象
    public static Fragment createFragment(int index) {
        //从集合中获取
        Fragment fragment = fragments.get(index);
        if (fragment == null) {
            switch (index) {
                case FRAGMENT_ONSALE:
                    fragment = new OnSaleFragment();
                    break;
                case FRAGMENT_BARGAINSALE:
                    fragment = new BargainSaleFragment();
                    break;
                case FRAGMENT_MAINSALE:
                    fragment = new MainSaleFragment();
                    break;
                case FRAGMENT_BRANDSALE:
                    fragment = new BrandSaleFragment();
                    break;
                case FRAGMENT_LOGIN:
                    fragment = new LoginFragment();
                    break;
            }
            //存到集合中
            fragments.put(index, fragment);
        }

        return fragment;
    }
}
